#include "VSMeshNode.h"
#include "VSSwitchNode.h"
#include "VSGraphicInclude.h"
#include "VSDebugDraw.h"
#include "VSViewFamily.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSMeshNode,VSNode)
BEGIN_ADD_PROPERTY(VSMeshNode,VSNode)
REGISTER_PROPERTY(m_ResourceName,ResourceName,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiRenderGroup,m_RenderGroup,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSMeshNode)
IMPLEMENT_INITIAL_END
VSMeshNode::VSMeshNode()
{
	m_bIsNeedDraw = true;
	m_bIsDrawBoundVolume = false;
	m_uiRenderGroup = VSCuller::RG_NORMAL;
	m_bIsVisibleUpdate = true;
}
VSMeshNode::~VSMeshNode()
{

}
void VSMeshNode::UpDateView(VSCuller & Culler,double dAppTime)
{
	
	VSNode::UpDateView(Culler,dAppTime);
	VSCamera * pCamera = Culler.GetCamera();
	if (!pCamera)
	{
		return ;
	}
	if (Culler.GetCullerType() == VSCuller::CUT_MAIN)
	{
		if (m_bIsDrawBoundVolume)
		{
			for (unsigned int i = 0 ; i < pCamera->GetViewFamilyNum() ;i++)
			{
				VSViewFamily * pViewFamily = pCamera->GetViewFamily(i);
				if (pViewFamily)
				{
					VSDebugDraw * pDebugDraw = pViewFamily->GetDebugDraw(m_uiRenderGroup);
					if (pDebugDraw)
					{
						pDebugDraw->AddDebugLineAABB(m_WorldBV,VSColorRGBA(1.0f,0.0f,0.0f,1.0f).GetDWARGB(),false);
					}
				}
			}		
		}
	}
}
unsigned int VSMeshNode::DiskUse()const
{
	unsigned int Used = VSNode::DiskUse();
	Used += VSResource::DiskUse();

	Used +=sizeof(unsigned int);
	return Used;
}

bool VSMeshNode::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if (!VSNode::Save(rStream,iSaveUse))
	{
		return 0;
	}
	if (!VSResource::Save(rStream,iSaveUse))
	{
		return 0;
	}
	if(!rStream.Write(&m_uiRenderGroup,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);
	return 1;
}
bool VSMeshNode::Load(VSStream & rStream,unsigned int &iSaveUse)
{
	if (!VSNode::Load(rStream,iSaveUse))
	{
		return 0;
	}
	if (!VSResource::Load(rStream,iSaveUse))
	{
		return 0;
	}
	if(!rStream.Read(&m_uiRenderGroup,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);
	return 1;
}
bool VSMeshNode::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSMeshNode* Temp = DynamicCast<VSMeshNode>(pObject); 
	if(!Temp)
		return 0;
	if(VSNode::Clone(pObject,uiType) && VSResource::Clone(Temp,uiType))
	{
		m_bIsDrawBoundVolume = Temp->m_bIsDrawBoundVolume;
		m_uiRenderGroup = Temp->m_uiRenderGroup;
		return 1;
	}
	else
	{
		return 0;
	}
}